'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('contact_persons', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      customer_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '客户ID'
      },
      name: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '联系人姓名'
      },
      department: {
        type: Sequelize.STRING(100),
        comment: '部门'
      },
      position: {
        type: Sequelize.STRING(100),
        comment: '职位'
      },
      phone: {
        type: Sequelize.STRING(20),
        comment: '电话'
      },
      email: {
        type: Sequelize.STRING(100),
        comment: '邮箱'
      },
      address: {
        type: Sequelize.STRING(200),
        comment: '地址'
      },
      is_primary: {
        type: Sequelize.INTEGER,
        defaultValue: 0,
        comment: '是否为主要联系人: 1是, 0否'
      },
      remark: {
        type: Sequelize.TEXT,
        comment: '备注'
      },
      created_at: {
        type: Sequelize.DATE
      },
      updated_at: {
        type: Sequelize.DATE
      }
    });

    // 添加索引
    await queryInterface.addIndex('contact_persons', ['customer_id'], {
      name: 'idx_contact_persons_customer_id'
    });

    await queryInterface.addIndex('contact_persons', ['is_primary'], {
      name: 'idx_contact_persons_is_primary'
    });

    await queryInterface.addIndex('contact_persons', ['name'], {
      name: 'idx_contact_persons_name'
    });
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('contact_persons');
  }
};